#if defined _td_register_included
	#endinput
#endif

#define _td_register_included

/*	This method registeres dealer with specified id
	@params:
		id -	dealer id must be less than MAX_DEALERS
		name - 	dealers name
		refreshItems -	should items be refreshed on round start
		refreshItemsCount -	if refreshItems = Dra_OnLost, specifies noumber of items dealer has to get below to get new items
		refreshItemsDropOld - 	specifies if dealer should loose current items on refresh
	@returns:
		id on success
		NO_DEALER if no dealer with specified id is already registered or id is more than MAX_DEALERS
*/
stock td_register_dealer_by_id(id , const name[MAX_DEALER_NAME_LEN], DealerRefreshAction:refreshItems = Dra_OnLost, refreshItemsCount = 0, bool:refreshItemsDropOld = false){
	if(!gDealerDef[id][Di_Registered]) {
		gDealerDef[id][Di_Registered] = 1;
		copy(gDealerDef[id][Di_Name], MAX_DEALER_NAME_LEN -1, name);		
		gDealerDef[id][Di_RefreshItems] = _:refreshItems;
		gDealerDef[id][Di_RefreshItemsMinCount] = refreshItemsCount;
		gDealerDef[id][Di_RefreshItemsDropOld] = refreshItemsDropOld;
		return id;
	}
	return NO_DEALER;
}

/*	This method registeres dealer with first free id
	@params:
		name - 	dealers name
		refreshItems -	should items be refreshed on round start
		refreshItemsCount -	if refreshItems = Dra_OnLost, specifies noumber of items dealer has to get below to get new items
		refreshItemsDropOld - 	specifies if dealer should loose current items on refresh
	@returns:
		dealers id on success
		NO_DEALER if no dealer with specified id is already registered or id is more than MAX_DEALERS
*/
stock td_register_dealer(const name[MAX_DEALER_NAME_LEN], DealerRefreshAction:refreshItems = Dra_OnLost, refreshItemsCount = 0, bool:refreshItemsDropOld = false){
	for(new id=0;id<MAX_DEALERS;id++){
		if(td_register_dealer_by_id(id, name, refreshItems, refreshItemsCount, refreshItemsDropOld) != NO_DEALER)
			return id;
	}
	return NO_DEALER;
}

/*	This method sets dealers item definition
	@params:
		dealer -	dealers id
		item - 		item
		minCount - 	minimum count of items
		maxCount - 	maximum count of items
		minChance - 	minimum chance dealer will have this item
		maxChance - 	maximum chance dealer will have this item
		minCost - 	minimum cost of item
		maxCost	- 	maximum cost of item		
*/
stock td_set_dealer_item_def(dealer, item, minCount, maxCount, minChance, maxChance, minCost, maxCost){
	gDealersItemsDefs[dealer][item][Ii_MinCount] = minCount;
	gDealersItemsDefs[dealer][item][Ii_MaxCount] = maxCount;
	gDealersItemsDefs[dealer][item][Ii_MinChance] = minChance;
	gDealersItemsDefs[dealer][item][Ii_MaxChance] = maxChance;
	gDealersItemsDefs[dealer][item][Ii_MinCost] = minCost;
	gDealersItemsDefs[dealer][item][Ii_MaxCost] = maxCost;
}

/*	This method giwes new items to all dealers (if forceGive = true or dealer needs items)
	@params:
		forceGive -	if true weapons will be given, else items will be given id dealer needs them
		forceDropOld -	if true forces dgopping old items
*/
stock td_give_items_to_dealers(bool:forceGive, bool:forceDropOld){
	for(new dealer=0;dealer<MAX_DEALERS;dealer++)
		td_give_items_to_dealer( dealer, forceGive, forceDropOld);
}

/*	This method giwes new items to a dealer (if forceGive = true or dealer needs items)
	@params:
		dealer - 	dealer id
		forceGive -	if true weapons will be given, else items will be given id dealer needs them
		forceDropOld -	if true forces dgopping old	 items
*/
stock td_give_items_to_dealer(dealer, bool:forceGive=false, bool:forceDropOld=false){
	if(!gDealerDef[dealer][Di_Registered])
		return;
	new bool:give = forceGive;
	if(!give){
		switch(gDealerDef[dealer][Di_RefreshItems]){
			case Dra_OnLost: if(td_get_dealers_items_count(dealer) <= gDealerDef[dealer][Di_RefreshItemsMinCount]){
				give = true;
			}
			case Dra_Always: give = true;
		}
	}
	if(give) {
		new bool:clear = forceDropOld;
		if(!forceDropOld)
			clear= bool:gDealerDef[dealer][Di_RefreshItemsDropOld];
		for(new item=0;item<Items;item++){
			if(clear)
				gDealersItems[dealer][item][Dii_Count] = 0;
			if(random_num(gDealersItemsDefs[dealer][item][Ii_MinChance],  gDealersItemsDefs[dealer][item][Ii_MaxChance]) < random_num(0, 100)){
				gDealersItems[dealer][item][Dii_Count] += random_num(gDealersItemsDefs[dealer][item][Ii_MinCount], gDealersItemsDefs[dealer][item][Ii_MaxCount]);
				gDealersItems[dealer][item][Dii_Cost] = random_num(gDealersItemsDefs[dealer][item][Ii_MinCost], gDealersItemsDefs[dealer][item][Ii_MaxCost]);
			}
		}
	}
}
/*	This method sets dealer's item
	@params:
		dealer - 	dealers id
		item - 		item
		bAdd - 		if true - dealers items count will be increased, else it will be set to count value
		count - 	noumber of items
		cost - 		cost of item
*/
stock td_give_item_to_dealer(dealer, item, bool:bAdd, count, cost){
	if(bAdd)
		gDealersItems[dealer][item][Dii_Count] += count
	else 
		gDealersItems[dealer][item][Dii_Count] = count;
	gDealersItems[dealer][item][Dii_Cost] = cost;
}

/*	This method sets available status for items
*/
stock td_set_player_items(id, bool:set=false){
	for(new item=0;item<Items; item++)
		playerAvailableItems[id][item] = set;
}

/*	This method sets available status for dealers
*/
stock td_set_player_dealers(id, bool:set=false){
	for(new dealer=0;dealer<MAX_DEALERS; dealer++)
		if(gDealerDef[dealer][Di_Registered])
			playerAvailableDealers[id][dealer] = set;
}

stock td_set_player_dealer(id, dealer, bool:enable){
	playerAvailableDealers[id][dealer] = enable
}
